# str_value = '@{url}'
# old_string = '@{url}'
# new_string = 'http://192.168.17.236:30510/'
# str_value = str_value.replace(old_string, new_string)
# print(str_value)
import requests
from bs4 import BeautifulSoup


def get_jd_product_info(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
    }

    # 发送GET请求
    response = requests.get(url, headers=headers)

    # 检查响应状态码
    if response.status_code == 200:
        print(response.text)
        soup = BeautifulSoup(response.text, 'html.parser')
        # print(soup)
        # 解析产品标题
        title = soup.find('div', {'class': 'sku-name'}).text.strip() if soup.find('div',
                                                                                  {'class': 'sku-name'}) else None

        # 解析产品价格
        price = soup.find('span', {'class': 'p-price'}).find('i').text if soup.find('span',
                                                                                    {'class': 'p-price'}) else None

        # 返回解析到的信息
        return {
            'title': title,
            'price': price
        }
    else:
        print(f"Failed to retrieve data. Status code: {response.status_code}")
        return None


# 测试函数
if __name__ == '__main__':
    url = 'https://item.jd.com/100011493273.html'  # 这里替换为你想要抓取的商品页面URL
    product_info = get_jd_product_info(url)
    if product_info:
        print(product_info)
